package com.linjacy.container.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linjacy.common.utils.PageUtils;
import com.linjacy.common.utils.Query;
import com.linjacy.container.product.dao.*;
import com.linjacy.container.product.entity.LogisticsEntity;
import com.linjacy.container.product.entity.MerchandiseEntity;
import com.linjacy.container.product.entity.TransporterEntity;
import com.linjacy.container.product.service.LogisticsService;
import com.linjacy.container.product.service.MerchandiseService;
import com.linjacy.container.product.service.OrderService;
import com.linjacy.container.product.service.TransporterService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("logisticsService")
public class LogisticsServiceImpl  extends ServiceImpl<LogisticsDao, LogisticsEntity> implements LogisticsService {
    @Autowired
    OrderService orderService;
    @Autowired
    OrderDao orderDao;
    @Autowired
    UserDao userDao;
    @Autowired
    TransporterDao transporterDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<LogisticsEntity> wrapper=new QueryWrapper<>();
        String logisticsId = (String) params.get("logistics_id");
        if(!StringUtils.isEmpty(logisticsId)){
            wrapper.like("logistics_id",logisticsId);
        }
        wrapper.ne("logistics_status",9);
        IPage<LogisticsEntity> page = this.page(
                new Query<LogisticsEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void updateStatus(Map<String, Object> params) {
        String oId = (String) params.get("orderId");
        String logisticsId = (String) params.get("logisticsId");
        String tId = (String) params.get("transporterId");
        String status1 = (String) params.get("status");
        long status = Long.parseLong(status1)+1;
        long orderId = Long.parseLong(oId);
        long transporterId = Long.parseLong(tId);
        if(status<9){
            baseMapper.updateLogisticsStatus(logisticsId,status);
        }else{
            baseMapper.updateLogisticsStatus(logisticsId,status);
            orderService.changeOrderStatus(orderId);
            transporterDao.changeTransporterStatus(0,transporterId);
            Long userId = orderDao.getUidByCid(orderId);
            int count = orderDao.selectCountByUid(userId);
            if(count<=0){
                userDao.updateOrderStatus(userId);
            }
        }

    }
}
